
//
//  GSSubscribeViewController_Add_Topic_Details_Web.m
//  GameSky
//
//  Created by lanou on 16/1/12.
//  Copyright © 2016年 NiRongfei. All rights reserved.
//

#import "GSSubscribeViewController_Add_Topic_Details_Web.h"
#import "GSArticleADView.h"
#import "GSNewsArticleModel.h"
#import "GSNewsCorrelationModel.h"

@interface GSSubscribeViewController_Add_Topic_Details_Web () <UITableViewDataSource,UITableViewDelegate,UIWebViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;// 背景ScrollView
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) GSArticleADView *adView;

@property (nonatomic, strong) GSNewsArticleModel *articleModel;

@property (nonatomic, strong) NSMutableArray *listDataArray;

@end

@implementation GSSubscribeViewController_Add_Topic_Details_Web
#pragma mark-------------------视图将要出现--------------------
- (void)viewWillAppear:(BOOL)animated{
    // 隐藏  backButton
    self.navigationItem.hidesBackButton = YES;
    self.navigationController.navigationBar.hidden = YES;
    // 隐藏 tabBar
    self.tabBarController.tabBar.hidden = YES;
    // 打开 隐藏的 toolBar
    self.navigationController.toolbar.hidden = NO;
    self.navigationController.toolbar.frame = CGRectMake(0, kScreenHeight - 44, kScreenWidth, 44);
    self.navigationController.toolbar.backgroundColor = [UIColor yellowColor];
}
#pragma mark-------------------视图将要消失------------------
- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.toolbar.hidden = NO;
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.toolbar.hidden = YES;
}
#pragma mark-------------视图加载完成-----------------------
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
// 初始化数据源数据
    _listDataArray = [NSMutableArray array];
// 解析数据
    [self setUpData];
// 添加  展示页面
    [self addSubviews];
// 添加 navigationBar
    [self setupNavigationBar];
}
#pragma mark-------------解析数据---------------------------
- (void)setUpData{
// 加载webView 数据
    [GSNetworkManager requestWithUrlStr:Subscribe_Add_Topic_Details_Web paraDict:_dic method:POST success:^(NSURLResponse *response, NSData *data) {
        if (data) {
            NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            NSString *str = dic[@"result"][@"mainBody"];

            str = [str stringByReplacingOccurrencesOfString:@"550" withString:@"250"];
            // model赋值
            self.articleModel = [[GSNewsArticleModel alloc] init];
            [self.articleModel setValuesForKeysWithDictionary:dic];
            // 设置 HTML图片尺寸
            NSString *htmlStr = [NSString stringWithFormat:@"<head><style>img{width:%.0fpx !important;}</style></head>%@",kScreenWidth - 30,str];
            htmlStr = [NSString stringWithFormat:@"<div id=\"webview_content_wrapper\">%@</div>",htmlStr];
            // webView 加载HTML字符串
            [self.webView loadHTMLString:htmlStr baseURL:nil];
        }
       
    } failure:^(NSError *error) {
        
    }];
// 加载广告数据
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:_dic];
    dict[@"request"] = @{};
    [GSNetworkManager requestWithUrlStr:@"http://appapi2.gamersky.com/v2/addetails" paraDict:dict method:POST success:^(NSURLResponse *response, NSData *data) {
        if (data) {
            NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            NSDictionary *resultDict = dataDict[@"result"];
            GSNewsADModel *model = [[GSNewsADModel alloc] init];
            [model setValuesForKeysWithDictionary:resultDict];
            self.adView.model = model;
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
// 加载相关数据
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:_dic];
    dictionary[@"request"] = @{@"contentId":_dic[@"request"][@"contentId"], @"contentType":@"news", @"pageIndex":@1, @"elementsCountPerPage":@5};
    [GSNetworkManager requestWithUrlStr:@"http://appapi2.gamersky.com/v2/TwoCorrelation" paraDict:dictionary method:POST success:^(NSURLResponse *response, NSData *data) {
        if (data) {
            NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            NSArray *resultArray = dataDict[@"result"];
            for (NSDictionary *subDict in resultArray) {
                GSNewsCorrelationModel *model = [[GSNewsCorrelationModel alloc] init];
                [model setValuesForKeysWithDictionary:subDict];
                [self.listDataArray addObject:model];
            }
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];

    
}
#pragma mark--------------设置自定义导航栏-------------------
- (void)setupNavigationBar {
    GSCustomNavigationBar *navBar = [[GSCustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    [self.view addSubview:navBar];
    // 左边按钮
    [navBar.leftButton setImage:[UIImage imageNamed:@"common_Icon_Back_20x20_UIMode_Day"] forState:(UIControlStateNormal)];
    [navBar.leftButton addTarget:self action:@selector(leftBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    // 右边按钮
    [navBar.rightButton setImage:[UIImage imageNamed:@"common_Icon_More_20x20_UIMode_Day"] forState:(UIControlStateNormal)];
    [navBar.rightButton addTarget:self action:@selector(rightBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
}
// 点击方法
- (void)leftBtnClick:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)rightBtnClick:(UIButton *)button {
    
}
#pragma mark-----------------展示视图----------------------
- (void)addSubviews {
    self.automaticallyAdjustsScrollViewInsets = NO;
    // 1.添加ScrollView
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 44)];
    
    self.scrollView.alwaysBounceVertical = YES;
    self.scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollView];
    // 2.添加WebView
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64 - 44)];
    self.webView.delegate = self;
    self.webView.scrollView.scrollEnabled = NO;
    
    
    [self.scrollView addSubview:self.webView];
    // 3.添加ADView
    self.adView = [[GSArticleADView alloc] initWithFrame:CGRectMake(0, self.webView.bottom, kScreenWidth, 100)];
    [self.scrollView addSubview:self.adView];
    // 4.添加TableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:(UITableViewStylePlain)];
    // 夜间模式
    self.tableView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
    self.tableView.scrollEnabled = NO;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 30;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    [self.scrollView addSubview:self.tableView];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
    label.text = @"相关阅读";
    label.font = [UIFont systemFontOfSize:15];
    self.tableView.tableHeaderView = label;
}

#pragma mark---------------UIWebView代理方法------------------
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
    // 设置WebView的Frame;
    webView.frame = CGRectMake(0, 64, kScreenWidth, height);
    // 设置ADView的Frame;
    self.adView.frame = CGRectMake(0, webView.bottom, kScreenWidth, 100);
    // 设置TableView的Frame;
    self.tableView.frame = CGRectMake(0, self.adView.bottom, kScreenWidth, self.tableView.tableHeaderView.height + self.listDataArray.count * self.tableView.rowHeight);
    // 设置ScrollView的ContentSize;
    self.scrollView.contentSize = CGSizeMake(kScreenWidth, self.tableView.bottom);
}
#pragma mark-------------------tableView协议-------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.text = [self.listDataArray[indexPath.row] title];
    return cell;
}

#pragma mark-----------------------------------------------
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
